<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2020-02-17
 * Time: 14:36
 */

namespace App\Services\System;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

class LogService
{
    /**
     * @var \App\Repositories\System\LogRepository $logRepository
     */
    private $logRepository;

    public function __construct()
    {
        $this->logRepository = app('logRepository');
    }

    public function log($actionid, $title, $content) {
        $data = [];
        $data["affairtypeid"] = "";
        $data["affairtypename"] = "";
        $data["eventtypeid"] = "";
        $data["eventtypename"] = "";
        $data["actionid"] = $actionid;
        $data["actionname"] = "";
        $data["logtitle"] = $title;
        $data["logcontent"] = $content;
        $data["optime"] = date( "Y-m-d H:i:s" );

        $user = Auth::user();
        $data["group_id"] = $user->group_id;
        $data["opuserid"] = $user->id;
        $data["opusertitle"] = $user->user_id;
        $data["opusername"] = $user->nickname;
        /** @var \App\Services\System\GroupService $groupService */
        $groupService = app('groupService');
        $grouprow = $groupService->getCore($data["group_id"]);
        if($grouprow){
            $grouprow = $grouprow->toArray();
            $data["group_name"] = $grouprow['group_name'];
        }

        $menu = DB::table('menus')->where('id',$actionid)->first();
        if($menu){
            $data["actionname"] = $menu->name;
        }
     

        // /** @var \App\Services\System\AffairtypeService $affairtypeService */
        // $affairtypeService = app('affairtypeService');
        // /** @var \App\Services\System\EventtypeService $eventtypeService */
        // $eventtypeService = app('eventtypeService');
        // /** @var \App\Services\System\OperationService $operationService */
        // $operationService = app('operationService');
        // $operationrow = $operationService->getCore($actionid);
        // if($operationrow){
        //     $operationrow = $operationrow->toArray();
        //     $data['actionname'] = $operationrow['actionname'];
        //     $data['eventtypeid'] = $operationrow['eventtypeid'];
        //     $eventtyperow = $eventtypeService->getCore($data['eventtypeid']);
        //     if($eventtyperow){
        //         $eventtyperow = $eventtyperow->toArray();
        //         $data['eventtypename'] = $eventtyperow['eventtypename'];
        //         $data['affairtypeid'] = $eventtyperow['affairtypeid'];

        //         $affairtyperow = $affairtypeService->getCore($data['affairtypeid']);
        //         if($affairtyperow){
        //             $affairtyperow = $affairtyperow->toArray();
        //             $data['affairtypename'] = $affairtyperow['affairtypename'];
        //         }
        //     }
        // }
        $data['ip'] = $this->getClientIp();
        $this->logRepository->add($data);
    }

    public function getList($requestData)
    {
        $filters = [];
        // 搜索
        if(isset($requestData['filters']['keyword']) && !empty($requestData['filters']['keyword'])){
            $filters[] = [DB::raw("(eventtypename like '%{$requestData['filters']['keyword']}%' or actionname like '%{$requestData['filters']['keyword']}%' or logtitle like '%{$requestData['filters']['keyword']}%' or logcontent like '%{$requestData['filters']['keyword']}%' or opusertitle like '%{$requestData['filters']['keyword']}%' or opusername like '%{$requestData['filters']['keyword']}%')"),'1'];
        }
        if(isset($requestData['filters']['startdate']) && !empty($requestData['filters']['startdate'])){
            $filters[] = ['optime', '>=', date('Y-m-d',strtotime($requestData['filters']['startdate'])).' 00:00:00'];
        }

        if(isset($requestData['filters']['enddate']) && !empty($requestData['filters']['enddate'])){
            $filters[] = ['optime', '<=', date('Y-m-d',strtotime($requestData['filters']['enddate'])).' 23:59:59'];
        }

        $sorts = ['id' => 'desc'];

        $result = $this->logRepository->getList($filters,$sorts,$requestData['page']);

        return $result->toArray();
    }

    private function getClientIp()
    {
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }
        if (getenv('HTTP_X_REAL_IP')) {
            $ip = getenv('HTTP_X_REAL_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
            $ips = explode(',', $ip);
            $ip = $ips[0];
        } elseif (getenv('REMOTE_ADDR')) {
            $ip = getenv('REMOTE_ADDR');
        } else {
            $ip = '0.0.0.0';
        }

        return $ip;
    }
}
